package csc.nndp.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import csc.nndp.dao.User;
import csc.nndp.dao.UserDB;

/**
 * Servlet implementation class Login
 */
//@WebServlet("/logon")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub login logon lung tung la sao/ nhung ma cai register cung vậy mà regis đúng mà!
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getRequestDispatcher("/logon.jsp").forward(request, response);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	User user;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		try {
			user = UserDB.getUser(userName, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*boolean check=false;
		if(userName.equals("admin")&& password.equals("admin123")) check =true;
		if(userName.equals("user01")&& password.equals("P@ssword123")) check=true;
		if(userName.equals("user02")&&password.equals("P@ssword123")) check=true;*/
		if(user!=null)
		{
			String isRememberPass=request.getParameter("remember");
			if(isRememberPass!=null){
				Cookie userNameCookie=new Cookie("userName",request.getParameter("userName"));
				userNameCookie.setMaxAge(3600);
				response.addCookie(userNameCookie);
				
				Cookie passwordCookie = new Cookie("password", request.getParameter("password"));
				passwordCookie.setMaxAge(3600);
				response.addCookie(passwordCookie);				
			}
			HttpSession sess = request.getSession(false);
			sess.setAttribute("user", user);
			String navPage = "pages\\file_management.html";
			
			if(user.getRole().equals("admin")){
				navPage = "view-user";
			}
			RequestDispatcher dispatcher = request.getRequestDispatcher(navPage);
		    dispatcher.forward(request, response);
			/*if(userName.equals("admin")){
				RequestDispatcher dispatcher = request.getRequestDispatcher("view-user");
				   dispatcher.forward(request, response);
			
			}else{
				RequestDispatcher dispatcher = request.getRequestDispatcher("view-employees");
				dispatcher.forward(request, response);
			}
*/
			
			
		}else{
			request.setAttribute("message", "Wrong username/password or not match.");
			RequestDispatcher dispatcher = request.getRequestDispatcher("logon.jsp");
		    dispatcher.forward(request, response);
		}
		
		
		
		
		
	}

}
